/*

 Exercise 3.1 - Analogue Thermometer
 Created 22/04/2010 ---  By John Boxall --- http://tronixstuff.wordpress.com ---  CC by-sa v3.0 
 
 Task: Analogue thermometer with current temperature display, resettable minimum and maximum;
 climate control usage indicator
 Celsius version
 
 */


#include <Servo.h> 

Servo leftservo; // setup our two servos
Servo rightservo;

// define variables
float currentC = 0;
float minC = 0;
float maxC = 0;
float voltage = 0;
float sensor = 0;
float heateron = 15; // it’s ok to turn on the heater if the temperature is below this value
float airconon = 30; // it’s ok to turn on the air conditioner if the temperature is above this value
int pressed = 0; 
int pressed2 = 0;
int action = 0;
int rightpos = 90;
int leftpos = 90;
int wavehello = 0;

void setup() 
{

  leftservo.attach(10);  // attaches the left servo on pin 10 to the servo object 
  rightservo.attach(11);  // attaches the right servo on pin 11 to the servo object 

  // need to get temperature to initialise min/max
  sensor = analogRead(5);       // TMP36 sensor output pin is connected to Arduino analogue pin 5
  voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
  voltage = voltage-500;        // remove voltage offset
  currentC = voltage/10;         // convert millivolts to Celsius

  minC = currentC;
  maxC = currentC;

  // set up buttons on digital inputs
  pinMode (2, INPUT);
  pinMode (3, INPUT);

  // set up LEDs on digital outputs, these will indicate min, max or both for "reset?"
  pinMode (5, OUTPUT); // "minimum" indicator
  pinMode (6, OUTPUT); // "maximum" indicator

}


int calculateservo(float temperature) 
// this function will compute the angle to send the servo to, based on the temperature
// we will use a range of 0~40 degrees celsius, with a servo that uses 180 for full left and 0 for full right
// linear algebra gives us the formula y=-4.5x+180; y is the servo value, x is the temperature
{
  float resulta;
  int resultb;
  resulta = -4.5 * temperature;
  resulta = resulta + 180;
  resultb = int(resulta);                //cannot send a floating number to a servo
  return resultb;
}

void loop() 
{

  digitalWrite (5, LOW);
  digitalWrite (6, LOW); 
  //  turn off LEDs

  // read current temperature
  sensor = analogRead(5);       // TMP36 sensor output pin is connected to Arduino analogue pin 5
  voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
  voltage = voltage-500;        // remove voltage offset
  currentC = voltage/10;         // convert millivolts to Celsius

  if (currentC<minC) 
  {
    minC = currentC;
  }
  if (currentC>maxC) 
  {
    maxC = currentC;
  }

  // display current temperature on rightservo

  rightpos = calculateservo(currentC); // convert temperature to a servo position
  rightservo.write(rightpos);         // move the right-hand servo to point to the current temperature


  // display climate situation on leftservo
  if (currentC>=airconon)
  {
    leftservo.write(35);
  }
  else if (currentC<=heateron)
  {
    leftservo.write(145);
  } 
  else
  {
    leftservo.write(90);
  }


  delay(200);

  // has the user pressed reset button?

  pressed = digitalRead(2);
  if (pressed == HIGH)
  {
    pressed = 0;
    action = 0;
    digitalWrite (5, HIGH); // turn on both LEDs, this means to the user "Reset min/max?"
    digitalWrite (6, HIGH); 
    delay(500); // gives time to ignore switch bounce
    while (action == 0) // loop around until the user presses yes or no for reset 
    {
      if (digitalRead(2)==HIGH)  // check yes button
      {
        minC = currentC;
        maxC = currentC;
        action = 1;// get me out of this while loop and back to normal operation
        delay(500); // gives time to ignore switch bounce
      }
      if (digitalRead(3)==HIGH) // check no button
      {
        action = 1; // get me out of this while loop and back to normal operation
        delay(500); // gives time to ignore switch bounce
      }

    } 
    digitalWrite (5, LOW); // turn off both LEDs
    digitalWrite (6, LOW); 
  }


  // has the user pressed min/max button?

  pressed = digitalRead(3);
  if (pressed == HIGH)
  {
    pressed = 0;
    // display minimum temperature
    digitalWrite (5, HIGH); // turn on "minimum" LED

    // display minimum temperature on rightservo
    rightpos = calculateservo(minC); // convert temperature to a servo position
    rightservo.write(rightpos);         // move the right-hand servo to point to the minimum temperature
    delay(3000); // wait
    digitalWrite (5, LOW); // turn off "minimum" LED

    // display maximum temperature

    digitalWrite (6, HIGH); // turn on "maximum" LED
    // display maximum temperature on rightservo
    rightpos = calculateservo(maxC); // convert temperature to a servo position
    rightservo.write(rightpos);         // move the right-hand servo to point to the minimum temperature
    delay(3000); // wait
    digitalWrite (6, LOW); // turn off "minimum" LED

  }
}







